<?php

namespace App\Trip\Orders\BookOrders\Plane\Order;

use App\Trip\Orders\BookOrders\Params\AbstractOrderParam\AbstractOrderParam;
use App\Trip\Orders\BookOrders\Params\AbstractParam\AbstractParam;
use App\Trip\Orders\BookOrders\Params\PassengersParam;
use App\Trip\Orders\BookOrders\Plane\Passengers\BookPlaneOrderPassengers;
use App\Trip\Orders\BookOrders\Plane\PriceDetails\BookPlanePriceDetails;
use App\Trip\Orders\TripOrderState;
use App\Trip\Orders\TripType;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use JetBrains\PhpStorm\ArrayShape;

class BookNewPlaneOrder
{
    private AbstractOrderParam $order_param;
    private AbstractParam      $param;
    private PassengersParam    $passenger_param;

    public function __construct(AbstractOrderParam $order_param, AbstractParam $param)
    {
        $this->param           = $param;
        $this->order_param     = $order_param;
        $this->passenger_param = $param->getPassengersParam();
    }


    /**
     * @throws Exception
     * @throws GuzzleException
     */
    #[ArrayShape([
        'channel_id'               => "int",
        'trip_id'                  => "string",
        'order_sn'                 => "string",
        'out_order_sn'             => "string",
        'user_id'                  => "string",
        'transfer_flag'            => "int",
        'trip_no'                  => "int",
        'order_service'            => "string",
        'online'                   => "int",
        'out_order_id'             => "string",
        'trip_type'                => "int",
        'order_state'              => "int",
        'order_snap_state'         => "int",
        'change_flag'              => "int",
        'order_amount'             => "float",
        'service_pay_amount'       => "float",
        'construct_fee'            => "float",
        'fuel_fee'                 => "float",
        'receivable'               => "float",
        'origin_order_need_refund' => "float",
        'plane_info'               => "array",
        'passengers'               => "array",
        'price_details'            => "array",
        'selected_seats'           => "string",
        'mobile'                   => "string",
        'snap_up'                  => "int",
        'seat_name'                => "string",
    ])] public function buildOrderInfo(): array
    {
        $plane_info          = $this->order_param->getTripParam()->toArray();
        $priceDetailsBuilder = $this->priceDetailsBuilder();
        return [
            'trip_id'                  => $this->order_param->getTripId(),
            'channel_id'               => channel()->getChannelId(),
            'order_sn'                 => $this->order_param->generateOrderSn(),
            'out_order_sn'             => $this->order_param->getOutOrderSn(),
            'user_id'                  => $this->param->getUserId(),
            'transfer_flag'            => $this->order_param->getTransferFlag(),
            'trip_no'                  => $this->order_param->getTripNo(),
            'order_service'            => $this->order_param->OrderService(),
            'online'                   => $this->order_param->getOnline(),
            'out_order_id'             => '',
            'trip_type'                => TripType::TRIP_TYPE_PLANE,
            'snap_up'                  => TripOrderState::ORDER_SNAP_NONE,
            'order_snap_state'         => TripOrderState::ORDER_SNAP_STATE_NONE,
            'order_state'              => TripOrderState::ORDER_STATE_WAITING_PAY,
            'change_flag'              => TripOrderState::ORDER_CHANGE_FLAG_NONE,
            'order_amount'             => $priceDetailsBuilder->getOrderAmount(),
            'service_pay_amount'       => 0,                                               //  服务商端 总需支付金额 支付后更新
            'provider_price_state'     => 1,                                               //  供应商价格  0 默认待确认 1 已确认
            'construct_fee'            => $priceDetailsBuilder->getTotalConstructionFee(), //  总机建费
            'fuel_fee'                 => $priceDetailsBuilder->getTotalFuelFee(),         //  总燃油费
            'receivable'               => $priceDetailsBuilder->getOrderAmount(),
            'origin_order_need_refund' => $priceDetailsBuilder->calcOriginNeedRefundAmount(),
            'plane_info'               => $plane_info,
            'passengers'               => $priceDetailsBuilder->getPassengers(),
            'price_details'            => $priceDetailsBuilder->getPriceDetails(),
            'mobile'                   => $this->param->getMobile(),
        ];
    }

    /**
     * @throws GuzzleException
     */
    public function priceDetailsBuilder(): BookPlanePriceDetails
    {
        return new BookPlanePriceDetails($this);
    }

    /**
     * @throws GuzzleException
     * @throws Exception
     */
    public function passengers(): array
    {
        return $this->passengerBuilder()->buildPassengers();
    }

    /**
     * @throws Exception
     */
    public function passengerBuilder(): BookPlaneOrderPassengers
    {
        return new BookPlaneOrderPassengers($this);
    }

    public function getOrderParam(): AbstractOrderParam
    {
        return $this->order_param;
    }


    public function getPassengerParam(): PassengersParam
    {
        return $this->passenger_param;
    }

    public function getWholeParam(): AbstractParam
    {
        return $this->param;
    }
}